<?php
namespace Admin\Controller;
header('Content-Type:text/html;charser=utf-8');
class UserController extends AuthController {

    /* 前台用户管理 */
    public function index(){  

		$array = $this->page(M('user a'));
		
		if(IS_POST){	//总删除;	
			if(!empty($_POST['id'])){
				for($i=0;$i<count(I('post.id'));$i++){
					$user = M('user')->where('id="'.$_POST['id'][$i].'"')->find();
					if(is_file(THINK_PATH .'../Public/' .$user['image_id']) && $user['image_id'] != 'Uplodes/image_id/59424ce0073a5.jpg'){//判断头像的路径是否为文件
						unlink(THINK_PATH .'../Public/' .$user['image_id']);//删除头像图片;
					}
					$del = M('user')->where('id="'.$_POST['id'][$i].'"')->delete();
				}
				if($del){
					$this->success('删除成功!');
				}
			}
        }	
        
		if(IS_GET){//分类查询
			if(empty($_GET['coupon'])){
				unset($_GET['coupon']);
			}else{
				$get['coupon'] = $_GET['coupon'];
			}
			
			if(empty($_GET['age'])){
				unset($_GET['age']);
			}else{
				$get['age'] = $_GET['age'];
			}
			
			if(empty($_GET['sex'])){
				unset($_GET['sex']);
			}else{
				$get['sex'] = $_GET['sex'];
			}
			
			if(empty($_GET['keywords'])){
				unset($_GET['keywords']);
				$array = $this->page(M('user a'),$get);
			}else{
				//模糊查询关键字
				$keywords = $_GET['keywords'];
				if(is_numeric($keywords)){
					$data['id|qq|degree|age|balance|integral|tel'] = array('like', "%$keywords%");
				}else{
					$data['nickname|user_name|sex_name|coupon_name|email|position|achievement|degree_name'] = array('like', "%$keywords%");
				}
				$array = $this->page(M('user a'),$data);
				//dump($array);
				foreach($array['list'] as $key=>$val){
					foreach($val as $k=>$v){
						 if($v == $keywords){
							 $array['list'][$key][$k] = "<font style='color:red;'>".$keywords."</font>";
						 }
					}
				}
			}	
			$this->assign('get',$_GET);// 赋值分类默认选择;			
		}
		
		
		$this->assign('huser',$array['list']);// 赋值数据集
		$this->assign('page',$array['show']);// 赋值分页输出
		$this->assign('coupon',M('coupon')->select());//会员
		$this->assign('sex',M('sex')->select());//性别
        $this->display('huser');
    }
	
	

    /* 添加前端用户 */
    public function huser_add(){
        if(IS_POST){
           $user = D('user');
            if($user->create()){
                if($user->add()){
                    $this->success('添加成功',U('user/index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($user->getError());
            }
        }
        $this->display('huser_add');
    }
    /* 用户是否存在验证 */
    public function add_ajax(){
        $user=M('user')->where('user_name="'.I('post.user_name').'"')->find();
        if(empty($user)){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }

    /* 删除前台用户 */
    public function huser_del(){
        if(IS_GET){
            $user = M('user')->where('id = "'.I('get.id').'"')->find();
            if(is_file(THINK_PATH .'../Public/' .$user['image_id']) && $user['image_id'] != 'Uplodes/image_id/59424ce0073a5.jpg'){//判断头像的路径是否为文件
                unlink(THINK_PATH .'../Public/' .$user['image_id']);//删除头像图片;
            }
            if( M('user')->where('id = "'.I('get.id').'"')->delete()){
                $this->success('删除成功',U('user/index'));
            }else{
                $this->error('删除失败');
            }
        }
    }

    /* 修改前端用户 */
    public function huser_select(){
        if(IS_GET){
            //dump(I('get.'));
            $this->assign(
				'user',
				$user = M('user a')
				->join('LEFT JOIN dx_sex b ON a.sex=b.sex_id')
				->join('LEFT JOIN dx_coupon c ON a.coupon=c.coupon_id')
				->join('LEFT JOIN dx_degree d ON a.degree=d.degree_id')
				->where('id="'.I('get.id').'"')->find()
			);
        }
        $this->display('huser_select');
    }
	
	
	/* 修改密码 */
	
	public function pass(){
		if(IS_POST){
			if(md5(I('post.mpass')) == $_SESSION['admin_user']['password']){
				if(I('post.newpass') == I('post.renewpass')){
					if(M('admin_user')->where('id="'.$_SESSION['admin_user']['id'].'"')->save(array('password' => md5(I('post.newpass'))))){
						$_SESSION['admin_user']['password'] = md5(I('post.newpass'));
						$this->success('修改密码成功',U('user/index'));
						
					}else{
						$this->error('修改密码失败');
					}
				}else{
					$this->error('确认密码不正确');
				}
			}else{
				$this->error('请输入正确的原始密码');
			}
		}
		$this->assign('admin_user',session('admin_user'));
		$this->display('pass');
	}
	
	/* 修改密码 */
	public function pass_ajax(){
		if(IS_POST){
			if(md5(I('post.password')) == $_SESSION['admin_user']['password']){
				$this->ajaxReturn(1);
			}
		}
	}
	
	/* 后台管理员 */
	public function auser(){
		$array = $this->page_admin(M('admin_user a'));
		$this->assign('page',$array['show']);
		$this->assign('user',$array['list']);
		$this->display('auser');
		
	}
	/* 后台管理员删除 */
	public function auser_del(){
		if(IS_GET){
			if(M('admin_user')->where('id="'.I('get.id').'"')->delete()){
				
				$this->success('删除成功',U('user/auser'));
			}else{
				$this->error('删除失败');
			}
		}
	}
	
	/* 添加后台用户 */
	public function admin_add(){
		
		if(IS_POST){
			if(D('AdminUser')->create()){
				if(D('AdminUser')->add()){
					$this->success('添加成功',U('user/auser'));
				}else{
					$this->error('添加失败');
				}
			}else{
				$this->error(D('AdminUser')->getError());
			}
		}
		$this->assign('auth',M('auth_group')->select());
		$this->display('admin_add');
	}
	public function add_admin_ajax(){
		$user=M('admin_user')->where('user_name="'.I('post.user_name').'"')->find();
        if(empty($user)){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
	}

}